package com.ajview.core.web.service;

import com.ajview.common.core.constant.SecurityConstant;
import com.ajview.common.core.domain.eneity.SysUser;
import com.ajview.core.config.model.SecurityModel;
import com.ajview.system.service.SysMenuService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;


/**
 * @author ZhangJunJie
 * @Date 2022-06-29
 * <p>
 * 概要：rbac权限信息
 */
@Component
public class SysPermissionService {

    @Resource
    private SysMenuService sysMenuServiceImpl;
    @Resource
    private SecurityModel securityModel;

    /**
     * 获取当前用户所拥有的的所有功能权限字符串
     * 如果是超管则返回 SecurityConstant.ALL_PERMISSION（*:*:*）代表所有权限
     * 非超管，用当前用户id查询数据库
     *
     * @param sysUser
     * @return
     */
    public Set<String> getMenuPermission(SysUser sysUser) {
        Set<String> perms = new HashSet<>();
        if (securityModel.isSystemUser(sysUser.getUserId())) {
            //是系统超级管理员
            perms.add(SecurityConstant.ALL_PERMISSION);
        } else {
            perms.addAll(sysMenuServiceImpl.selectMenuListByUserId(sysUser.getUserId()));
        }
        // 获取该用户的所有菜单权限
        return perms;
    }
}
